package Reference;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.nio.cs.ext.SJIS;

import javax.swing.text.html.StyleSheet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("巨潮网络");
    static MongoCollection collection = mDatabase.getCollection("2020_07_01___2020_12_31");

    public static void main(String[] args) throws IOException, InvalidFormatException {
        Filter();
    }

    public static void Filter() throws IOException, InvalidFormatException {
        Map<String, Document> data_map = new HashMap<>();
        FindIterable<Document> documents = collection.find();

        for (Document document : documents){
            String title = document.getString("title");
            if (title.contains("摘要") || title.contains("补正") || title.contains("更正") || title.contains("补充") ||
                title.contains("翻译") || title.contains("英文") || title.contains("取消"))
                continue;

            data_map.put(document.getString("secCode"), document);
        }


        List<String> set_list = new ArrayList<>();
        set_list.addAll(documents.first().keySet());
        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        Row row = sheet.createRow(0);
        int index = 0;
        int n = set_list.size();
        for (int i = 0; i < n; i++)
            row.createCell(index++).setCellValue(set_list.get(i));

        int rownum = 1;
        int count = 0;
        for (Map.Entry<String, Document> map : data_map.entrySet()) {
            int cellnum = 0;

            count++;
            Row row1 = sheet.createRow(rownum++);
            for (int i = 0; i < n; i++) {
                row1.createCell(cellnum++).setCellValue(map.getValue().get(set_list.get(i)).toString());
            }

            System.out.println("正在写入------------------------------");
        }

//        workbook.write(new FileOutputStream("C:\\Users\\jinghe\\192.168.1.173\\高新技术企业\\excel导出\\高新技术企业\\认定.xlsx"));
        workbook.write(new FileOutputStream("D:\\测试临时文件\\巨潮网络_筛选后.xls"));
        System.out.println("写去excel完毕*--------------------");
        System.out.println("条目数量：" + count);

    }


}


